<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="审批人">
        <el-input v-model="formInline.user" placeholder="审批人"></el-input>
      </el-form-item>
      <el-form-item label="活动区域">
        <el-select v-model="formInline.region" placeholder="活动区域">
          <el-option label="区域一" value="shanghai"></el-option>
          <el-option label="区域二" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="success" @click="Create">新增</el-button>
        <el-button type="success" @click="BulkDelete">删除</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="adminId" label="用户ID" width="180">
      </el-table-column>
      <el-table-column prop="userName" label="用户名" width="180">
      </el-table-column>
      <el-table-column prop="avatar" label="头像">
        <template slot-scope="row">
          <img :src="imgUrl(row.row.avatar)" width="30">
        </template>
      </el-table-column>
      <el-table-column prop="createTime" :formatter="formatter" label="时间">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="添加管理员" :visible.sync="dialogTableVisible">
      <admin-create ref="adminCreate" @OnSave="SaveData"></admin-create>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogTableVisible = false">取 消</el-button>
        <el-button type="primary" @click="Save">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="编辑管理员" :visible.sync="dialogTableVisible_Edit" :key="new Date().getTime()">
      <admin-edit ref="adminEdit" :adminId="adminId" @OnSave="SaveData"></admin-edit>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogTableVisible_Edit = false">取 消</el-button>
        <el-button type="primary" @click="SaveUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import axios from '../../util/request';
  import AdminCreate from './AdminCreate';
  import AdminEdit from './AdminEdit';
  import moment from 'moment'
  export default {
    components: {
      AdminCreate,
      AdminEdit
    },
    data() {
      return {
        adminIds: [],
        tableData: [],
        formInline: {
          user: '',
          region: ''
        },
        adminId: 0,
        dialogTableVisible: false,
        dialogTableVisible_Edit: false
      }
    },
    mounted() {
      this.LoadData();
    },
    methods: {
      /**
       * @description: 获取图片路径
       * @param {*} path
       * @return {*}
       */
      imgUrl(path) {
        return `http://localhost:5000${path}`;
      },
      onSubmit() {
        console.log('submit!');
      },
      Create() {
        this.dialogTableVisible = true;
      },
      Save() {
        this.$refs.adminCreate.submitForm('ruleForm');
      },
      SaveUpdate() {
        this.$refs.adminEdit.submitForm('ruleForm');
      },
      LoadData() {
        axios.get("/adminlist").then(m => {
          this.tableData = m.data.result.data;
        });
      },
      SaveData() {
        this.dialogTableVisible = false;
        this.dialogTableVisible_Edit = false;
        this.LoadData();
      },
      /**
       * @description: 格式化
       * @param {*} row
       * @param {*} column
       * @param {*} cellValue
       * @param {*} index
       * @return {*}
       */
      formatter(row, column, cellValue, index) {
        return moment(cellValue).lang('zh-cn').format('YYYY-MM-DD HH:mm:ss Q dddd');
      },
      handleEdit(index, row) {
        this.dialogTableVisible_Edit = true;
        this.adminId = row.adminId;
      },
      handleDelete(index, row) {
        axios.get('/admin/delete?id=' + row.adminId).then(m => {
          this.LoadData();
        });
      },
      handleSelectionChange(val) {
        this.adminIds = val;
        console.log(this.adminIds);
      },
      BulkDelete() {
        axios.post('/admin/bulkdelete', this.adminIds.map(m => m.adminId)).then(m => {

        });
      }
    },
  }
</script>